Java EE 7 Essentials by Arun Gupta

Java EE 7 Essentials by Arun Gupta

Author:Arun Gupta [Arun Gupta]
Language: eng
Format: epub, pdf
Tags: COMPUTERS / Programming Languages / Java
ISBN: 9781449370169
Publisher: O’Reilly Media
Published: 2013-08-08T16:00:00+00:00


Events

Events provide an annotation-based event model based upon the observer pattern. Event producers raise events that are consumed by observers. The event object, typically a POJO, carries state from producer to consumer. The producer and the observer are completely decoupled from each other and only communicate using the state.

A producer bean will fire an event using the Event interface:

@Inject @Any Event<Customer> event; //. . . event.fire(customer);

An observer bean with the following method signature will receive the event:

void onCustomer(@Observes Customer event) { //. . . }

In this code, Customer is carrying the state of the event.

The producer bean can specify a set of qualifiers when injecting the event:

@Inject @Any @Added Event<Customer> event;

The observer bean’s method signature has to match the exact set of qualifiers in order to receive the events fired by this bean:

void onCustomer(@Observes @Added Customer event) { //. . . }

Qualifiers with parameters and multiple qualifiers may be specified to further narrow the scope of an observer bean.

By default, an existing instance of the bean or a new instance of the bean is created in the current context to deliver the event. This behavior can be altered so that the event is delivered only if the bean already exists in the current scope:

void onCustomer( @Observes( notifyObserver= Reception.IF_EXISTS) @Added Customer event){ //. . . }

Transactional observer methods receive their event notifications during the before completion or after completion phases of the transaction in which the event was fired. TransactionPhase identifies the kind of transactional observer methods, as defined in Table 9-6.

Table 9-6. Transactional observers

Transactional observersDescription

IN_PROGRESS Default behavior; observers are called immediately

BEFORE_COMPLETION Observers are called during the before completion phase of the transaction

AFTER_COMPLETION Observers are called during the after completion phase of the transaction

AFTER_FAILURE Observers are called during the after completion phase of the transaction, only when the transaction fails

AFTER_SUCCESS Observers are called during the after completion phase of the transaction, only when the transaction succeeds



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.